<?php
declare (strict_types = 1);
namespace app\index\model\user;

use think\Model;

/**
 * @mixin \think\Model
 */
class UserCard extends Model
{
    protected $pk = "id";

    /**
     * 获取信息
     * @param $where
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getInfoByWhere($where){
        return $this->where($where)->find();
    }

    /**
     * 获取信息
     * @param $id
     * @param $addWhere
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getInfoById($id,$addWhere = []){
        $where = ['id'=>$id,'is_delete'=>1];
        return $this->where($where)->where($addWhere)->find();
    }

    /**
     * 获取指定用户的卡信息
     * @param $userId
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getUserCardInfo($userId){
        $where = ['user_id'=>$userId,'is_delete'=>1];
        return $this->where($where)->find();
    }

    /**
     * 添加用户卡信息
     * @param $userId
     * @param $data
     * @return bool|int|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function addUserCard($userId,$data){
        $getCardNumber = $this->getCardNumber();
        if($getCardNumber['code'] > 0) return false;
        $getLogicalNumber = $this->getLogicalNumber();
        if($getLogicalNumber['code'] > 0) return false;
        $userCard = [
            'user_id'=>$userId,
            'card_number'=>$getCardNumber['data'],
            'card_password'=>password_hash($data['pwd'], PASSWORD_BCRYPT),
            'balance'=>0,
            'status'=>1,
            'user_money'=>0,
            'is_delete'=>1,
            'create_time'=>date("Y-m-d H:i:s"),
            'update_time'=>date("Y-m-d H:i:s"),
            'logical_card'=>$getLogicalNumber['data'],
        ];
        return $this->insertGetId($userCard);
    }

    /**
     * 返回充电卡的逻辑号(16位)
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getLogicalNumber(){
        $status = true;
        $i = 1;
        $backData = ['code'=>1,'msg'=>'获取失败'];
        while($status){
            if($i>20){
                $status = false;
                break;
            }else{
                $getNumber = $this->getNumber();
                $checkInfo = $this->where('is_delete',1)->where('card_number',$getNumber)->find();
                if(!$checkInfo){
                    $backData['code'] = 0;
                    $backData['data'] = $getNumber;
                    $status = false;
                    break;
                }
            }
            $i++;
        }
        return $backData;
    }

    /**
     * 返回充电卡的逻辑号(16位)
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getCardNumber(){
        $status = true;
        $i = 1;
        $backData = ['code'=>1,'msg'=>'获取失败'];
        while($status){
            if($i>20){
                $status = false;
                break;
            }else{
                $getNumber = $this->getNumber();
                $checkInfo = $this->where('is_delete',1)->where('logical_card',$getNumber)->find();
                if(!$checkInfo){
                    $backData['code'] = 0;
                    $backData['data'] = $getNumber;
                    $status = false;
                    break;
                }
            }
            $i++;
        }
        return $backData;
    }

    /**
     * @return string
     * @author xaboy
     * @day 2020/6/9
     */
    public function getNumber()
    {
        list($msec, $sec) = explode(' ', microtime());
        $msectime = date("ymdHis");
        $orderNumber = $msectime . mt_rand(1000,9999);
        return $orderNumber;
    }

}
